package com.like.hospitalworld;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.GetUserInfoReq;
import com.like.entity.GetUserInfoResp;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.RetrofitUtil;
import com.like.network.UserService;
import com.like.utils.Constants;
import com.like.utils.ImageUtils;
import com.makeramen.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_personal_center)
public class PersonalCenterActivity extends BaseActivity {

    public static final String LOG_OUT = "tag_log_out";

    @ViewInject(R.id.user_icon)
    RoundedImageView mUserIcon;
    @ViewInject(R.id.username)
    TextView mUsername;

    private UserService mUserService;
    private GetUserInfoResp mUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserService = RetrofitUtil.getService(UserService.class);
        getUserInfo();
    }

    private void getUserInfo() {
        GetUserInfoReq req = new GetUserInfoReq(m.getLoginResp().userID);
        Call<GeneralResponse<GetUserInfoResp>> call = mUserService.getUserInfo(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<GetUserInfoResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<GetUserInfoResp>> call, Response<GeneralResponse<GetUserInfoResp>> response) {
                if (!response.isSuccessful())
                    return;
                mUserInfo = response.body().data;
                ImageUtils.showImage(m.mContext, mUserInfo.head, m.getToken(), mUserIcon);
                mUsername.setText(mUserInfo.nickName);
            }

            @Override
            public void onFailure(Call<GeneralResponse<GetUserInfoResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Event(R.id.person_info)
    private void clickPersonInfo(View view) {
        if (mUserInfo == null)
            return;
        Intent intent = new Intent(PersonalCenterActivity.this, PersonalInfoActivity.class);
        startActivity(intent);
    }

    @Event(R.id.person_collection)
    private void clickCollection(View view) {
        Intent intent = new Intent(PersonalCenterActivity.this, CollectionActivity.class);
        startActivity(intent);
    }

    @Event(R.id.person_count)
    private void clickIntegral(View view) {
        Intent intent = new Intent(PersonalCenterActivity.this, IntegralActivity.class);
        startActivity(intent);
    }

    @Event(R.id.person_download)
    private void clickDownload(View view) {
        Intent intent = new Intent(PersonalCenterActivity.this, OfflineDownloadActivity.class);
        startActivity(intent);
    }

    @Event(R.id.message_layout)
    private void clickMessage(View view) {
        Intent intent = new Intent(PersonalCenterActivity.this, MessageActivity.class);
        startActivity(intent);
    }

    @Event(R.id.feedback)
    private void feedbackClick(View v) {
        Intent intent = new Intent(m.mContext, FeedBackActivity.class);
        startActivity(intent);
    }

    @Event(R.id.logout)
    private void logoutClick(View v) {
        m.logout();
        Intent intent = new Intent(m.mContext, LoginActivity.class);
        startActivity(intent);
        EventBus.getDefault().post(new EventWrapper(null, LOG_OUT));
        finish();
    }
}
